package org.blogwu.learn.algorithm.array.arrayList;


import java.util.Arrays;

/**
 * 动态数组
 *
 * @author zsyubo
 */
public class DynamicArray<E> {

    /**
     * 元素数量
     */
    private int size = 0;

    /**
     * 默认容量
     */
    private static final int DEFAULT_CAPACITY = 10;


    /**
     * 所有对的元素
     */
    private Object[] elements;


    public DynamicArray() {
        this(DEFAULT_CAPACITY);
    }

    public DynamicArray(int size) {
        if (size < DEFAULT_CAPACITY) size = DEFAULT_CAPACITY;
        this.elements = new Object[size];
    }

    // 元素数量
    public int size() {
        return this.size;
    }

    /**
     * 是否为空
     *
     * @return
     */
    public boolean isEmtpty() {
        return size == 0;
    }

    /**
     * 返回索引出的元素
     *
     * @return
     */
    E get(int index) {
        if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index:" + index);
        return (E) elements[index];
    }

    /**
     * 根据下标设定指定元素
     *
     * @param index
     * @param element
     * @return
     */
    E set(int index, E element) {
//        if ( element == null ) return null;
        if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index:" + index);
        E old = (E) elements[index];
        elements[index] = element;
        return old;
    }

    /**
     * 查看元素的为知
     *
     * @param element
     * @return
     */
    public int indexOf(E element) {
        for (int i = 0; i < size; i++) {
            if (element.equals(elements[i])) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 是否包含某个元素
     *
     * @param element
     * @return
     */
    public boolean contains(E element) {
        return this.indexOf(element) > -1;
    }

    /**
     * 添加一个元素
     *
     * @param element
     */
    public void add(E element) {
        if (elements.length <= size) {
            System.out.println("扩容了");
            // 扩容
            int newSize = elements.length * 2;
            Object[] newArray = new Object[newSize];
            elements = Arrays.copyOf(elements, newSize);
        }
        elements[size++] = element;
    }


    /**
     * 在指定位置添加元素
     *
     * @param index   添加的下标
     * @param element
     */
    public void add(int index, E element) {
        // index = 5 ---> 添加index的
        if (index < 0 || index > size) throw new IndexOutOfBoundsException("Index:" + index);
        if (index == size) {
            this.add(element);
        }
        if (elements.length <= size) {
            // 扩容
            System.out.println("扩容了");
            // 扩容
            int newSize = elements.length * 2;
            Object[] newArray = new Object[newSize];
            elements = Arrays.copyOf(elements, newSize);
        }

        for (int i = size; i > index; i--) {
            elements[i] = elements[i - 1];
        }
        elements[index] = element;
        size++;
    }

    /**
     * 删除指定位置的元素
     *
     * @param index
     * @return
     */
    public E remove(int index) {
        if (index < 0 || index >= size) throw new IndexOutOfBoundsException("Index:" + index);
        E old = (E) elements[index];
        elements[index] = null;
        for (int i = index + 1; i < size; i++) {
            elements[i - 1] = elements[i];
            elements[i] = null;
        }
        size--;
        return old;
    }

    /**
     * 清除所有元素
     */
    void clear() {
        // 吧引用置为NUll
        for (int i = 0; i < elements.length; i++) {
            elements[i] = null;
        }
        this.size = 0;
    }
}